import { axios } from '../../request/myAxios'
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tablist: [
      {
        id: 1,
        name: "全部"
      }, {
        id: 2,
        name: "待付款"
      }, {
        id: 3,
        name: "待发货"
      }
    ],
    ordersList: [],
    showtop: false,
    activeIndex: 0


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type || 1;
    this.getOrderData(type);
    // 更新activeIndex
    this.setData({
      activeIndex: type - 1
    })
  },

  getChildIndex(e) {
    const { index } = e.detail;
    // 获取对应的数据
    let type = index + 1;
    //第二种写法
    // const {tablist}=this.data;
    // type=tablist[index].id
    this.getOrderData(type);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getOrderData(type = 1) {
    axios({
      url: "/my/orders/all",
      method: "get",
      data: {
        type
      }
    }).then(res => {
      console.log("全部订单", res);
      const { orders } = res;
      const ordersList = orders.map(item => {
        return {
          order_id: item.order_id,
          order_number: item.order_number,
          total_price: item.total_price,
          create_time: util.formatTime(new Date(item.create_time * 1000))
        }
      })
      console.log("订单列表", ordersList);
      //  更新视图数据
      this.setData({
        ordersList
      })
    })
  },
  //返回顶部的功能
  onPageScroll({ scrollTop }) {
    this.setData({
      showtop: scrollTop >= 200 ? true : false
    })
  }
})